package org.mech.sefer.service.rmi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.remoting.rmi.RmiServiceExporter;

/**
 * <p>
 * The RmiServiceExporterFactory is factory for creating RmiServiceExporter
 * objects. It is regular Spring bean that register new RmiServiceExporter
 * definition into spring context, so later spring handle them as regular beans
 * </p>
 * <p>
 * Date: 23.5.2012 16:41:31
 * </p>
 * 
 * @author martin.krajc
 */
public class RmiServiceExporterFactory extends AbstractRmiBeanDefinitionRegistrator{

	/**
	 * The logger.
	 */
	private final Logger logger = LoggerFactory.getLogger(RmiServiceExporterFactory.class);


	@Override
	protected BeanDefinition prepareBeanDefinition(Class<?> serviceInterface, String beanName, String serviceName) {
		final GenericBeanDefinition beanDef = new GenericBeanDefinition();
		beanDef.setBeanClass(RmiServiceExporter.class);

		final MutablePropertyValues values = new MutablePropertyValues();
		values.addPropertyValue("registryPort", getRmiRegistryPort());
		values.addPropertyValue("service", new RuntimeBeanReference(beanName));
		values.addPropertyValue("serviceName", serviceName);
		values.addPropertyValue("serviceInterface", serviceInterface);

		beanDef.setPropertyValues(values);

		logger.debug("preparing bean definition for ['" + serviceInterface.getName() + "']");

		return beanDef;
	}

	@Override
	protected String prepareBeanId(Class<?> serviceInterface, String beanName) {
		return "rse#" + beanName;
	}

}
